using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using System;
namespace MicroMsg.Network
{
	public class SessionPackMgr
	{
		private static Queue mSessionPackQueue = new Queue();
		private static byte[] mSessionKey = null;
		private static AccountInfo mAccountInfo = new AccountInfo();
		private static int mAuthStatus = 0;
		private static object mSessionPackLock = new object();
		private static object mSessionKeyLock = new object();
		private static object mSeverIDLock = new object();
		public static string mAuthTicket1 = null;
		public static string mAuthTicket2 = null;
		public static AccountInfo getAccount()
		{
			return SessionPackMgr.mAccountInfo;
		}
		public static int getAuthStatus()
		{
			return SessionPackMgr.mAuthStatus;
		}
		public static void setAuthStatus(int int_0)
		{
			SessionPackMgr.mAuthStatus = int_0;
		}
		public static bool isAuthing()
		{
			return SessionPackMgr.mAuthStatus == 1;
		}
		public static byte[] getSeverID()
		{
			byte[] result;
			lock (SessionPackMgr.mSeverIDLock)
			{
				int num = SessionPackMgr.mAccountInfo.getCookie().Length;
				byte[] array = new byte[num];
				Arrays.fill(array, 0);
				Buffer.BlockCopy(SessionPackMgr.mAccountInfo.getCookie(), 0, array, 0, num);
				result = array;
			}
			return result;
		}
		public static void setSeverID(byte[] serverID)
		{
			lock (SessionPackMgr.mSeverIDLock)
			{
				byte[] array = new byte[serverID.Length];
				Buffer.BlockCopy(serverID, 0, array, 0, serverID.Length);
				SessionPackMgr.mAccountInfo.setCookie(array);
			}
		}
		public static byte[] getSessionKeyEx()
		{
			byte[] result;
			lock (SessionPackMgr.mSessionKeyLock)
			{
				byte[] array = new byte[36];
				Arrays.fill(array, 0);
				if (SessionPackMgr.mSessionKey != null)
				{
					Buffer.BlockCopy(SessionPackMgr.mSessionKey, 0, array, 0, 36);
				}
				result = array;
			}
			return result;
		}
		public static byte[] getSessionKey()
		{
			if (SessionPackMgr.mSessionKey == null)
			{
				return null;
			}
			return SessionPackMgr.getSessionKeyEx();
		}
		public static bool isValidSessionKey()
		{
			bool result;
			lock (SessionPackMgr.mSessionKeyLock)
			{
				result = (SessionPackMgr.mSessionKey != null);
			}
			return result;
		}
		public static void setSessionKey(byte[] byte_0)
		{
			lock (SessionPackMgr.mSessionKeyLock)
			{
				if (byte_0 == null)
				{
					SessionPackMgr.mSessionKey = null;
				}
				else
				{
					if (SessionPackMgr.mSessionKey == null)
					{
						SessionPackMgr.mSessionKey = new byte[36];
						Arrays.fill(SessionPackMgr.mSessionKey, 0);
					}
					int num = byte_0.Length;
					if (num > 36)
					{
						num = 36;
					}
					Buffer.BlockCopy(byte_0, 0, SessionPackMgr.mSessionKey, 0, num);
				}
			}
		}
		public static bool hasSessionPack()
		{
			bool result;
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			return result;
		}
		public static void reset()
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				SessionPackMgr.mSessionPackQueue.reset();
			}
		}
		public static void cancelAllPacket()
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						sessionPack.mCanceled = true;
					}
				}
			}
		}
		public static SessionPack getFirstNotSended()
		{
			SessionPack result;
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					result = null;
				}
				else
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.mSendStatus == 0 && !sessionPack.mCanceled)
						{
							result = sessionPack;
							return result;
						}
					}
					result = null;
				}
			}
			return result;
		}
		public static void getPackWithHttpClient(Func<SessionPack, bool> predicate)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.mHttpClient != null)
						{
							predicate.Invoke(sessionPack);
						}
					}
				}
			}
		}
		public static void closeAllHttpClientInPacks()
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.mHttpClient != null)
						{
							sessionPack.mHttpClient.close();
							sessionPack.mHttpClient = null;
						}
					}
				}
			}
		}
		public static int clearCompletedOrCancel()
		{
			int result;
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					result = 0;
				}
				else
				{
					int num = 0;
					ListNode next = SessionPackMgr.mSessionPackQueue._head._next;
					ListNode listNode = SessionPackMgr.mSessionPackQueue._head;
					while (next != null)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.mSendStatus < 5 && !sessionPack.mCanceled)
						{
							listNode = next;
						}
						else
						{
							if (sessionPack.mSendStatus < 5 && sessionPack.mCanceled)
							{
								OnCallback.onError(sessionPack, PackResult.BEEN_CANCELLED);
							}
							listNode._next = next._next;
							SessionPackMgr.mSessionPackQueue._size--;
							num++;
						}
						next = next._next;
					}
					SessionPackMgr.mSessionPackQueue._tail = listNode;
					result = num;
				}
			}
			return result;
		}
		public static void markAllToNotSended(int connMode)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.mSendStatus <= 3 && (sessionPack.mConnectMode & connMode) != 0)
						{
							sessionPack.mSendStatus = 0;
						}
					}
				}
			}
		}
		public static void cleanAllTimeoutPoint(int connMode)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if ((sessionPack.mConnectMode & connMode) != 0)
						{
							sessionPack.cancelTimeoutPoint();
						}
					}
				}
			}
		}
		public static SessionPack getSessionPackBySeq(int int_0)
		{
			SessionPack result;
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					result = null;
				}
				else
				{
					result = (SessionPackMgr.mSessionPackQueue.method_0(int_0) as SessionPack);
				}
			}
			return result;
		}
		public static void changeSessionPackSeq(int oldSeq, int newSeq)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					SessionPackMgr.mSessionPackQueue.changeID(oldSeq, newSeq);
				}
			}
		}
		public static void checkSendTimeout()
		{
			SessionPack sessionPack = SessionPackMgr.checkSendTimeoutEx();
			if (sessionPack != null)
			{
				Sender.getInstance().onSendTimeout(sessionPack);
			}
		}
		private static SessionPack checkSendTimeoutEx()
		{
			SessionPack result;
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					result = null;
				}
				else
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack.isSendTimeout())
						{
							result = sessionPack;
							return result;
						}
					}
					result = null;
				}
			}
			return result;
		}
		public static void checkPackTimeout()
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					ListNode next = SessionPackMgr.mSessionPackQueue._head._next;
					double nowMilliseconds = Util.getNowMilliseconds();
					while (next != null)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack != null && sessionPack.mSendStatus <= 3)
						{
							sessionPack.checkPackTimeout(nowMilliseconds);
						}
						next = next._next;
					}
				}
			}
		}
		public static void getPackEx(Func<SessionPack, bool> predicate)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				if (!SessionPackMgr.mSessionPackQueue.isEmpty())
				{
					for (ListNode next = SessionPackMgr.mSessionPackQueue._head._next; next != null; next = next._next)
					{
						SessionPack sessionPack = next._obj as SessionPack;
						if (sessionPack != null)
						{
							predicate.Invoke(sessionPack);
						}
					}
				}
			}
		}
		public static void putToHead(SessionPack sessionPack)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				SessionPackMgr.mSessionPackQueue.putToHead(sessionPack, sessionPack.mSeqID);
			}
		}
		public static void putToTail(SessionPack sessionPack)
		{
			lock (SessionPackMgr.mSessionPackLock)
			{
				SessionPackMgr.mSessionPackQueue.putToTail(sessionPack, sessionPack.mSeqID);
			}
		}
	}
}
